<?php

// +----------------------------------------------------------------------
// | 用户
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\controller\api\v3;

use app\model\app\Modular as ModularModel;
use app\pay\event\UserBalance as UserBalanceEvent;
use app\pay\event\UserBalanceCash as UserBalanceCashEvent;
use app\model\app\Dynamic as DynamicModel;
use app\event\index\Share as ShareEvent;
use app\model\index\CreditcardProduct as CreditcardProductModel;
use think\Exception;
use think\facade\Log;
use think\Image as ImageThink;

class Share extends Common
{
    // 首页
//    public function index()
//    {
//        // 二维码
//        $event = new ShareEvent();
//        $data = $event->poster($this->user);
//        return $this->autoAjax($data);
//    }

    // 首页
    public function dynamic()
    {
        if ( 1 == $this->is_examine)
        {
            $data['lists'] = DynamicModel::with('image')->where('id',1655)->where('status', 1)->order('create_time DESC')->paginate();
        }else{
            $data['lists'] = DynamicModel::with('image')->where('status', 1)->where('is_show',1)->order('create_time DESC')->paginate();
        }
        $data['modular'] =  ['void'=>14];
        return $this->successAjax($data);
    }

    // 首页
    public function info()
    {
        $event = new ShareEvent();
        $data = $event->info($this->request->param(),$this->user);
        return $this->autoAjax($data);
    }
    // 首页
    public function card()
    {
        $event = new ShareEvent();
        $data = $event->card($this->user);
        return $this->autoAjax($data);
    }
    public function qun()
    {
        $data['qr_code_share'] = $this->request->domain().'/upload/qun.jpg';
        $data['url'] = $this->request->domain().'/upload/qun.jpg';
        $data['code_image'] = $this->request->domain().'/upload/qun.jpg';
        $data['register_url'] = $this->request->domain().'/upload/qun.jpg';
        $data['share_desc'] = '邀请加入云副业';
        $data['share_title'] = '邀请加入云副业';
        return $this->successAjax($data);
    }
    public function poster()
    {
        try {
            if (!$this->user)
            {
                throw new Exception('请先登录');
            }
            $id = input('id/d',0);
            $product_type = input('type/d',1);
            $productInfo = CreditcardProductModel::where('id',$id)->find();
            $uid = $this->user['id'];
            if (empty($productInfo)){
                throw new Exception('此产品已暂停申请');
            }
            $productInfo = $productInfo->getData();

            if (strpos($productInfo['poster_image'],'https://v3.mayichuangye168.com') !== false)
            {
                $productInfo['poster_image'] = str_replace('https://v3.mayichuangye168.com','',$productInfo['poster_image']);
            }
            if (strpos($productInfo['poster_image'],'https://v3.yunfuye.com') !== false)
            {
                $productInfo['poster_image'] = str_replace('https://v3.yunfuye.com/','',$productInfo['poster_image']);
            }

            if (!is_file(public_path().$productInfo['poster_image'])){
                throw new Exception('此产品暂无海报');
            }
            $productInfo['uid'] = $uid;
            $QR = '/upload/qrCode/cc/qrc_'.$this->user['shareCode'].'-'.$id.'.png';//已经生成的原始二维码图
//            $QR = '/upload/poster/code/' . time_format($this->user['create_time'], 'Ym/').'/qrc_'.$this->user['shareCode'].'-'.$id.'.png';//已经生成的原始二维码图
            switch ($product_type){
                case 3:
//                    $shareUrl = $this->request->domain().'/pages/index/apply?id='.$id.'&share_code='.$this->user['shareCode'];
                    $shareUrl = 'https://web.yunfuye.com/#/pages/index/apply?id='.$id.'&share_code='.$this->user['shareCode'];
                    break;
                default:
                    $shareUrl = 'https://web.yunfuye.com/#/pages/index/apply?id='.$id.'&share_code='.$this->user['shareCode'];
                    break;
            }
            if($productInfo['settlementType'] == 2){
//                $channel = (new Product())->getChannel();
//                if($channel){
//                    $shareUrl =  $productInfo['product_url'] . $channel;
//                }else{
//                    $this->error('服务器已爆满，请稍候再来');
//                }
                $shareUrl =  $productInfo['product_url'];
            }
            if(!is_file(public_path().$QR)){
                $QR = create_qr_code( $shareUrl,$QR,false,$id);
            }
            $posterPatch = public_path().$productInfo['poster_image'];
            $return_file = 'b_posters-'.$this->user['shareCode'].'-'.$id.'.png';
            if ($productInfo['type'] == 1){
                $posterUrl = createShare($posterPatch,$QR,public_path().'/upload/productPoster/cc/',$return_file,$id);
                $full_domain = $this->request->domain().'/upload/productPoster/cc/'.$posterUrl;
            }elseif ($productInfo['type'] == 2 || $productInfo['type'] == 3){
                $url = '/upload/productPoster/' . time_format(time(), 'Ym/') .$return_file;
                $dir = public_path() . 'upload/productPoster/' . time_format(time(), 'Ym/');
                if (!is_dir($dir)) {
                    //第三个参数是“true”表示能创建多级目录，iconv防止中文目录乱码
                    mkdir(iconv("UTF-8", "GBK", $dir), 0777, true);
                }
                if (is_file($dir.$return_file))
                {
                    unlink($dir.$return_file);
                }
                $qrFile =  ImageThink::open(public_path() . $QR);
                $qrFile->thumb(350, 350)->save(public_path().'upload/thumb.png');
                $image = ImageThink::open($posterPatch);

                $image = $image->water(public_path().'upload/thumb.png', [970, 2090]);
                $image->save($dir.$return_file);
                unlink(public_path().'upload/thumb.png');
                $full_domain = $this->request->domain().$url;
//                $posterUrl = createShare2($posterPatch,$QR,public_path().'/upload/productPoster/cc/',$return_file,$id);
            }else{
                throw new Exception('暂不支持');
            }
//            $posterUrl = createPoster('.'.$posterPatch,$QR,'/upload/productPoster/cc/'.$return_file);
            $data['code'] = 0;
            $data['data']['img'] = $full_domain.'?'.uniqid();
            $data['data']['url'] = $full_domain.'?'.uniqid();
            $data['data']['link'] = $shareUrl;
        }catch (\Exception $exception)
        {
            $data['code'] = 1;
            $data['message'] = $exception->getMessage();
        }
        return $this->autoAjax($data);
    }
}
